package com.dxy.app.widget.universal_view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import com.dxy.app.utils.ScreenUtils;
import java.util.List;

/**
  *Created by duxingyu on 2017/7/24.18:51
  *Email: 
  *Description:批量生成相似控件的LinearLayout，常用于代码生成界面或者控件时使用
  **/
public abstract class RegularLinearLayout<T> extends LinearLayout implements View.OnClickListener{
  public RegularLinearLayout(Context context) {
    super(context);
  }

  public RegularLinearLayout(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
  }

  public RegularLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  /**
   * 控件的数据源
   * */
  protected List<T> list;
  /**
   * 初始化数据源
   * */
  protected abstract void initDatas();
  public void setList(List<T> list){
    this.list =list;
    initViews();
  }
  /**
   * 创建视图
   * */
  private void initViews() {
    initDatas();
    if(list==null||list.size()==0){
      return;
    }
    setOrientation(VERTICAL);
    int perColumWidth = ScreenUtils.screenWidth / getPerLineCount();
    if (getPerLineCount() == 1) {
      for (int i = 0; i < list.size(); i++) {
        T data = list.get(i);
        addFuncBlock(this, i, data, perColumWidth, getPerColumHeight(i));
      }
    } else {
      int lineCount = list.size() / getPerLineCount();
      int last = list.size() % getPerLineCount();
      if (last != 0) {
        lineCount += 1;
      }
      for (int i = 0; i < lineCount; i++) {
        LinearLayout layout = new LinearLayout(getContext());
        layout.setOrientation(HORIZONTAL);
        int start = getPerLineCount() * i;
        int end;
        if (i != lineCount - 1) {
          end = getPerLineCount() * (i + 1);
        } else {
          end = list.size();
        }
        for (int j = start; j < end; j++) {
          addFuncBlock(layout, j, list.get(j), perColumWidth, getPerColumHeight(j % getPerLineCount()));
        }
        addView(layout);
        LayoutParams lps = (LayoutParams) layout.getLayoutParams();
        if(i!=lineCount-1){
          lps.setMargins(0,getPerLineMargin(),0,0);
        }else{
          lps.setMargins(0,getPerLineMargin(),0,getPerLineMargin());
        }
      }
    }
  }
  /**
   * 创建一个单元模块
   * @param group 单元的上级容器
   * @param pos 该数据单元的索引
   * @param data 该数据单元对应的数据
   * @param width 单元宽度
   * @param height 单元高度
   * */
  private void addFuncBlock(LinearLayout group,int pos,T data,int width,int height){
    View child =createUnit(pos,data);
    bindDataAndEvent(child,data);
    LayoutParams lps =getEachLayoutParams(width, height<=0?LayoutParams.WRAP_CONTENT:height);
    group.addView(child,lps);
  }
  /**
   * 创建单元控件
   * @param pos 该控件的索引
   * @param t 该控件绑定的数据
   * @return 返回对应的单元控件
   * */
  protected abstract View createUnit(int pos,T t);
  /**
   * 每行的个数
   * @return 获取每行显示控件的个数
   * */
  protected abstract int getPerLineCount();
  /**
   * 每个单元的高度
   * @param pos 当前单元的索引
   * @return 返回每个单元的高度
   * */
  protected abstract int getPerColumHeight(int pos);

  protected LinearLayout.LayoutParams getEachLayoutParams(int width,int height){
    LinearLayout.LayoutParams lps =new LinearLayout.LayoutParams(width,height);
    lps.gravity = Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL;
    return lps;
  }

  @Override
  public void onClick(View v) {
    if(listener==null){
      return;
    }
    T t = (T) v.getTag();
    listener.onItemClick(t);
  }

  /**
   * 每行之间的间隔
   * @return 返回每行之间的间隔
   * */
  protected int getPerLineMargin(){
    return 30;
  }
  /**
   * 绑定数据／事件
   * @param view 单元控件
   * @param t 绑定的数据
   * */
  protected void bindDataAndEvent(View view,T t){
    view.setTag(t);
    view.setOnClickListener(this);
  }
  protected OnItemClickListener<T> listener;

  public void setItemClickListenr(OnItemClickListener<T> listenr){
    this.listener =listenr;
  }

  public interface OnItemClickListener<T>{
    void onItemClick(T t);
  }
}
